{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "编写程序处理数据文件：yq_in.txt <br>\n",
    "要求： <br>\n",
    "1,生成可执行程序yq.exe; <br>\n",
    "2,命令行执行：yq yq_in.txt,得到结果如yq_out.txt所示 <br>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 打开yq_in.txt文件\n",
    "f_in = open(\"yq_in.txt\", 'r', encoding = 'gb2312') \n",
    "# 将文件内内容写入file\n",
    "file = f_in.read()\n",
    "f_in.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "list0=[]\n",
    "simple = []\n",
    "flag=0\n",
    "simple.append(file.split())  # file里的全部元素作为一个列表集合放入simple这个列表里\n",
    "for i in simple[0]:   # 遍历simple列表里第0个元素\n",
    "    list0.append(i)  # 将其添加到list0中"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "浙江省\n",
      "温州\t504\n",
      "台州\t146\n",
      "省十里丰监狱\t36\n",
      "杭州\t169\n",
      "宁波\t157\n",
      "嘉兴\t45\n",
      "金华\t55\n",
      "绍兴\t42\n",
      "舟山\t10\n",
      "丽水\t17\n",
      "衢州\t14\n",
      "湖州\t10\n",
      "\n",
      "江西省\n",
      "九江\t118\n",
      "南昌\t229\n",
      "上饶\t123\n",
      "新余\t130\n",
      "赣州\t76\n",
      "萍乡\t33\n",
      "宜春\t106\n",
      "抚州\t72\n",
      "吉安\t22\n",
      "鹰潭\t18\n",
      "景德镇\t6\n",
      "赣江新区\t1\n",
      "\n",
      "广东省\n",
      "深圳\t417\n",
      "广州\t346\n",
      "东莞\t97\n",
      "佛山\t84\n",
      "珠海\t98\n",
      "江门\t23\n",
      "中山\t66\n",
      "惠州\t62\n",
      "湛江\t22\n",
      "茂名\t14\n",
      "肇庆\t19\n",
      "梅州\t16\n",
      "阳江\t14\n",
      "汕头\t25\n",
      "韶关\t10\n",
      "揭阳\t8\n",
      "汕尾\t5\n",
      "潮州\t5\n",
      "清远\t12\n",
      "河源\t4\n",
      "待明确地区\t0\n",
      "\n",
      "江苏省\n",
      "南京\t93\n",
      "苏州\t87\n",
      "淮安\t66\n",
      "连云港\t48\n",
      "徐州\t79\n",
      "无锡\t55\n",
      "扬州\t23\n",
      "泰州\t37\n",
      "常州\t51\n",
      "南通\t40\n",
      "盐城\t27\n",
      "宿迁\t13\n",
      "镇江\t12\n",
      "\n",
      "湖南省\n",
      "长沙\t242\n",
      "岳阳\t156\n",
      "株洲\t80\n",
      "娄底\t76\n",
      "常德\t82\n",
      "邵阳\t102\n",
      "衡阳\t48\n",
      "湘潭\t36\n",
      "郴州\t39\n",
      "益阳\t59\n",
      "永州\t44\n",
      "怀化\t40\n",
      "湘西自治州\t8\n",
      "张家界\t5\n",
      "\n",
      "安徽省\n",
      "阜阳\t155\n",
      "合肥\t174\n",
      "蚌埠\t160\n",
      "六安\t69\n",
      "亳州\t108\n",
      "马鞍山\t38\n",
      "宿州\t41\n",
      "安庆\t83\n",
      "铜陵\t29\n",
      "淮南\t27\n",
      "芜湖\t33\n",
      "淮北\t27\n",
      "池州\t17\n",
      "滁州\t13\n",
      "黄山\t9\n",
      "宣城\t6\n",
      "\n",
      "陕西省\n",
      "西安\t120\n",
      "汉中\t26\n",
      "安康\t26\n",
      "渭南\t15\n",
      "铜川\t8\n",
      "商洛\t7\n",
      "咸阳\t17\n",
      "宝鸡\t13\n",
      "延安\t8\n",
      "榆林\t3\n",
      "韩城\t1\n",
      "杨凌\t1\n",
      "待明确地区\t0\n",
      "\n",
      "河南省\n",
      "信阳\t274\n",
      "郑州\t157\n",
      "南阳\t156\n",
      "周口\t76\n",
      "驻马店\t139\n",
      "平顶山\t58\n",
      "安阳\t53\n",
      "漯河\t35\n",
      "开封\t26\n",
      "商丘\t91\n",
      "许昌\t39\n",
      "濮阳\t17\n",
      "洛阳\t31\n",
      "新乡\t57\n",
      "鹤壁\t19\n",
      "焦作\t32\n",
      "济源\t5\n",
      "三门峡\t7\n",
      "待明确地区\t0\n",
      "\n",
      "贵州省\n",
      "遵义\t32\n",
      "贵阳\t36\n",
      "毕节\t23\n",
      "黔南州\t17\n",
      "六盘水\t10\n",
      "铜仁\t10\n",
      "黔东南州\t10\n",
      "安顺\t4\n",
      "黔西南州\t4\n",
      "\n"
     ]
    }
   ],
   "source": [
    "new_str = ''\n",
    "j = 0\n",
    "for i in list0:\n",
    "    if(j == 0):  #第一行数据读取\n",
    "        # 第一行一定是省份\n",
    "        new_str += i\n",
    "        new_str += '\\n'  # 省份做单独一行\n",
    "    elif(j%3 == 0):  # 省份 \n",
    "        # help_list.append(i)\n",
    "        if(new_str.find(i) == -1):  # 如果new_str中没有找到i这个省份\n",
    "            new_str += '\\n'  # 与之前的数据分格开来\n",
    "            new_str += i\n",
    "            new_str += '\\n'  # 省份做单独一行\n",
    "    elif(j%3 == 1):  # 市区\n",
    "        new_str += i\n",
    "        new_str += '\\t'\n",
    "    elif(j%3 == 2):  # 数量\n",
    "        new_str += i\n",
    "        new_str += '\\n'\n",
    "    j+=1\n",
    "print(new_str)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "f_out=open(\"yq_out.txt\",\"w+\")\n",
    "f_out.write(new_str)\n",
    "f_out.close()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
